﻿using System;
using System.Data;
using System.Data.SqlClient;
using APP.HELPER.Data.Helper;
using APP.HELPER.Utils.CustomizeToolbox;
using DevExpress.XtraEditors;
using DevExpress.XtraEditors.Controls;
using ComboBox = System.Windows.Forms.ComboBox;
using System.ComponentModel;
using DevExpress.XtraEditors.Repository;

namespace Dictionary.ERP
{
    public class DIC_MACHINE
    {
        private string m_Com;
        private string m_IP;
        private string m_MachineCode;
        private string m_MachineName;
        private string m_Password;
        private string m_PortID;
        private string m_PortType;

        public DIC_MACHINE()
        {
            m_MachineCode = "";
            m_MachineName = "";
            m_PortType = "";
            m_PortID = "";
            m_IP = "";
            m_Password = "";
            m_Com = "";
        }

        public DIC_MACHINE(string MachineCode, string MachineName, string PortType, string PortID, string IP,
            string Password, string Com)
        {
            m_MachineCode = MachineCode;
            m_MachineName = MachineName;
            m_PortType = PortType;
            m_PortID = PortID;
            m_IP = IP;
            m_Password = Password;
            m_Com = Com;
        }

        [DisplayName("Com"), Category("Column")]
        public string Com
        {
            get { return m_Com; }
            set { m_Com = value; }
        }

        [DisplayName("IP"), Category("Column")]
        public string IP
        {
            get { return m_IP; }
            set { m_IP = value; }
        }

        [Category("Primary Key"), DisplayName("MachineCode")]
        public string MachineCode
        {
            get { return m_MachineCode; }
            set { m_MachineCode = value; }
        }

        [Category("Column"), DisplayName("MachineName")]
        public string MachineName
        {
            get { return m_MachineName; }
            set { m_MachineName = value; }
        }

        [DisplayName("Password"), Category("Column")]
        public string Password
        {
            get { return m_Password; }
            set { m_Password = value; }
        }

        [Category("Column"), DisplayName("PortID")]
        public string PortID
        {
            get { return m_PortID; }
            set { m_PortID = value; }
        }

        [DisplayName("PortType"), Category("Column")]
        public string PortType
        {
            get { return m_PortType; }
            set { m_PortType = value; }
        }

        public string ProductName
        {
            get { return "Class DIC_MACHINE"; }
        }

        public string ProductVersion
        {
            get { return "1.0.0.0"; }
        }

        public void AddCheckedListBox(CheckedListBoxControl checkList)
        {
            var list = new DataTable();
            list = GetList();
            foreach (DataRow row in list.Rows)
            {
                var item = new CheckedListBoxItem
                {
                    Description =
                        row["MachineName"] + " - " + row["MachineCode"] + " (" +
                        DateTime.Parse(row["BeginTime"].ToString()).ToShortTimeString() + "->" +
                        DateTime.Parse(row["EndTime"].ToString()).ToShortTimeString() + ")",
                    Value = row["MachineCode"]
                };
                checkList.Items.Add(item);
            }
        }

        public void AddCombo(ComboBox combo)
        {
            GridCalendarControlLib.TableToComboBox(combo, GetList(), "MachineName", "MachineCode");
        }

        public void AddComboAll(ComboBox combo)
        {
            var list = new DataTable();
            list = GetList();
            DataRow row = list.NewRow();
            row["MachineCode"] = "All";
            row["MachineName"] = "Tất cả";
            list.Rows.InsertAt(row, 0);
            GridCalendarControlLib.TableToComboBox(combo, list, "MachineName", "MachineCode");
        }

        public void AddComboEdit(ComboBoxEdit combo)
        {
            var list = new DataTable();
            list = GetList();
            foreach (DataRow row in list.Rows)
            {
                combo.Properties.Items.Add(row["MachineCode"]);
            }
        }

        public void AddGridLookupEdit(GridLookUpEdit gridlookup)
        {
            var list = new DataTable();
            list = GetList();
            gridlookup.Properties.DataSource = list;
            gridlookup.Properties.DisplayMember = "MachineName";
            gridlookup.Properties.ValueMember = "MachineCode";
        }

        public void AddRepositoryGridLookupEdit(RepositoryItemGridLookUpEdit gridlookup)
        {
            var list = new DataTable();
            list = GetList();
            gridlookup.DataSource = list;
            gridlookup.DisplayMember = "MachineName";
            gridlookup.ValueMember = "MachineCode";
        }

        public string Delete()
        {
            string[] myParams = {"@MachineCode"};
            object[] myValues = {m_MachineCode};
            var helper = new SqlHelper();
            return helper.ExecuteNonQuery("DIC_MACHINE_Delete", myParams, myValues);
        }

        public string Delete(string MachineCode)
        {
            string[] myParams = {"@MachineCode"};
            object[] myValues = {MachineCode};
            var helper = new SqlHelper();
            return helper.ExecuteNonQuery("DIC_MACHINE_Delete", myParams, myValues);
        }

        public string Delete(SqlConnection myConnection, string MachineCode)
        {
            string[] myParams = {"@MachineCode"};
            object[] myValues = {MachineCode};
            var helper = new SqlHelper();
            return helper.ExecuteNonQuery(myConnection, "DIC_MACHINE_Delete", myParams, myValues);
        }

        public string Delete(SqlTransaction myTransaction, string MachineCode)
        {
            string[] myParams = {"@MachineCode"};
            object[] myValues = {MachineCode};
            var helper = new SqlHelper();
            return helper.ExecuteNonQuery(myTransaction, "DIC_MACHINE_Delete", myParams, myValues);
        }

        public bool Exist(string MachineCode)
        {
            bool hasRows = false;
            string[] myParams = {"@MachineCode"};
            object[] myValues = {MachineCode};
            var helper = new SqlHelper();
            SqlDataReader reader = helper.ExecuteReader("DIC_MACHINE_Get", myParams, myValues);
            if (reader != null)
            {
                hasRows = reader.HasRows;
                reader.Close();
                helper.Close();
                reader = null;
            }
            return hasRows;
        }

        public string Get(string MachineCode)
        {
            string str = "";
            string[] myParams = {"@MachineCode"};
            object[] myValues = {MachineCode};
            var helper = new SqlHelper();
            SqlDataReader reader = helper.ExecuteReader("DIC_MACHINE_Get", myParams, myValues);
            if (reader != null)
            {
                while (reader.Read())
                {
                    m_MachineCode =
                        Convert.ToString((reader["MachineCode"] == DBNull.Value) ? "" : reader["MachineCode"]);
                    m_MachineName =
                        Convert.ToString((reader["MachineName"] == DBNull.Value) ? "" : reader["MachineName"]);
                    m_PortType = Convert.ToString((reader["PortType"] == DBNull.Value) ? "" : reader["PortType"]);
                    m_PortID = Convert.ToString((reader["PortID"] == DBNull.Value) ? "" : reader["PortID"]);
                    m_IP = Convert.ToString((reader["IP"] == DBNull.Value) ? "" : reader["IP"]);
                    m_Password = Convert.ToString((reader["Password"] == DBNull.Value) ? "" : reader["Password"]);
                    m_Com = Convert.ToString((reader["Com"] == DBNull.Value) ? "" : reader["Com"]);
                    str = "OK";
                }
                reader.Close();
                helper.Close();
                reader = null;
            }
            return str;
        }

        public string Get(SqlConnection myConnection, string MachineCode)
        {
            string str = "";
            string[] myParams = {"@MachineCode"};
            object[] myValues = {MachineCode};
            var helper = new SqlHelper();
            SqlDataReader reader = helper.ExecuteReader(myConnection, "DIC_MACHINE_Get", myParams, myValues);
            if (reader != null)
            {
                while (reader.Read())
                {
                    m_MachineCode =
                        Convert.ToString((reader["MachineCode"] == DBNull.Value) ? "" : reader["MachineCode"]);
                    m_MachineName =
                        Convert.ToString((reader["MachineName"] == DBNull.Value) ? "" : reader["MachineName"]);
                    m_PortType = Convert.ToString((reader["PortType"] == DBNull.Value) ? "" : reader["PortType"]);
                    m_PortID = Convert.ToString((reader["PortID"] == DBNull.Value) ? "" : reader["PortID"]);
                    m_IP = Convert.ToString((reader["IP"] == DBNull.Value) ? "" : reader["IP"]);
                    m_Password = Convert.ToString((reader["Password"] == DBNull.Value) ? "" : reader["Password"]);
                    m_Com = Convert.ToString((reader["Com"] == DBNull.Value) ? "" : reader["Com"]);
                    str = "OK";
                }
                reader.Close();
                helper.Close();
                reader = null;
            }
            return str;
        }

        public string Get(SqlTransaction myTransaction, string MachineCode)
        {
            string str = "";
            string[] myParams = {"@MachineCode"};
            object[] myValues = {MachineCode};
            var helper = new SqlHelper();
            SqlDataReader reader = helper.ExecuteReader(myTransaction, "DIC_MACHINE_Get", myParams, myValues);
            if (reader != null)
            {
                while (reader.Read())
                {
                    m_MachineCode =
                        Convert.ToString((reader["MachineCode"] == DBNull.Value) ? "" : reader["MachineCode"]);
                    m_MachineName =
                        Convert.ToString((reader["MachineName"] == DBNull.Value) ? "" : reader["MachineName"]);
                    m_PortType = Convert.ToString((reader["PortType"] == DBNull.Value) ? "" : reader["PortType"]);
                    m_PortID = Convert.ToString((reader["PortID"] == DBNull.Value) ? "" : reader["PortID"]);
                    m_IP = Convert.ToString((reader["IP"] == DBNull.Value) ? "" : reader["IP"]);
                    m_Password = Convert.ToString((reader["Password"] == DBNull.Value) ? "" : reader["Password"]);
                    m_Com = Convert.ToString((reader["Com"] == DBNull.Value) ? "" : reader["Com"]);
                    str = "OK";
                }
                reader.Close();
                helper.Close();
                reader = null;
            }
            return str;
        }

        public DataTable GetList()
        {
            var helper = new SqlHelper();
            return helper.ExecuteDataTable("DIC_MACHINE_GetList");
        }

        public string Insert()
        {
            string[] myParams = {"@MachineCode", "@MachineName", "@PortType", "@PortID", "@IP", "@Password", "@Com"};
            object[] myValues = {m_MachineCode, m_MachineName, m_PortType, m_PortID, m_IP, m_Password, Com};
            var helper = new SqlHelper();
            return helper.ExecuteNonQuery("DIC_MACHINE_Insert", myParams, myValues);
        }

        public string Insert(SqlTransaction myTransaction)
        {
            string[] myParams = {"@MachineCode", "@MachineName", "@PortType", "@PortID", "@IP", "@Password", "@Com"};
            object[] myValues = {m_MachineCode, m_MachineName, m_PortType, m_PortID, m_IP, m_Password, Com};
            var helper = new SqlHelper();
            return helper.ExecuteNonQuery(myTransaction, "DIC_MACHINE_Insert", myParams, myValues);
        }

        public string Insert(string MachineCode, string MachineName, string PortType, string PortID, string IP,
            string Password, string Com)
        {
            string[] myParams = {"@MachineCode", "@MachineName", "@PortType", "@PortID", "@IP", "@Password", "@Com"};
            object[] myValues = {MachineCode, MachineName, PortType, PortID, IP, Password, Com};
            var helper = new SqlHelper();
            return helper.ExecuteNonQuery("DIC_MACHINE_Insert", myParams, myValues);
        }

        public string Insert(SqlConnection myConnection, string MachineCode, string MachineName, string PortType,
            string PortID, string IP, string Password, string Com)
        {
            string[] myParams = {"@MachineCode", "@MachineName", "@PortType", "@PortID", "@IP", "@Password", "@Com"};
            object[] myValues = {MachineCode, MachineName, PortType, PortID, IP, Password, Com};
            var helper = new SqlHelper();
            return helper.ExecuteNonQuery(myConnection, "DIC_MACHINE_Insert", myParams, myValues);
        }

        public string Insert(SqlTransaction myTransaction, string MachineCode, string MachineName, string PortType,
            string PortID, string IP, string Password, string Com)
        {
            string[] myParams = {"@MachineCode", "@MachineName", "@PortType", "@PortID", "@IP", "@Password", "@Com"};
            object[] myValues = {MachineCode, MachineName, PortType, PortID, IP, Password, Com};
            var helper = new SqlHelper();
            return helper.ExecuteNonQuery(myTransaction, "DIC_MACHINE_Insert", myParams, myValues);
        }

        public string NewID()
        {
            return SqlHelper.GenCode("DIC_MACHINE", "MachineCode", "");
        }

        public string Update()
        {
            string[] myParams = {"@MachineCode", "@MachineName", "@PortType", "@PortID", "@IP", "@Password", "@Com"};
            object[] myValues = {m_MachineCode, MachineName, m_PortType, m_PortID, m_IP, m_Password, Com};
            var helper = new SqlHelper();
            return helper.ExecuteNonQuery("DIC_MACHINE_Update", myParams, myValues);
        }

        public string Update(string MachineCode, string MachineName, string PortType, string PortID, string IP,
            string Password, string Com)
        {
            string[] myParams = {"@MachineCode", "@MachineName", "@PortType", "@PortID", "@IP", "@Password", "@Com"};
            object[] myValues = {MachineCode, MachineName, PortType, PortID, IP, Password, Com};
            var helper = new SqlHelper();
            return helper.ExecuteNonQuery("DIC_MACHINE_Update", myParams, myValues);
        }

        public string Update(SqlConnection myConnection, string MachineCode, string MachineName, string PortType,
            string PortID, string IP, string Password, string Com)
        {
            string[] myParams = {"@MachineCode", "@MachineName", "@PortType", "@PortID", "@IP", "@Password", "@Com"};
            object[] myValues = {MachineCode, MachineName, PortType, PortID, IP, Password, Com};
            var helper = new SqlHelper();
            return helper.ExecuteNonQuery(myConnection, "DIC_MACHINE_Update", myParams, myValues);
        }

        public string Update(SqlTransaction myTransaction, string MachineCode, string MachineName, string PortType,
            string PortID, string IP, string Password, string Com)
        {
            string[] myParams = {"@MachineCode", "@MachineName", "@PortType", "@PortID", "@IP", "@Password", "@Com"};
            object[] myValues = {MachineCode, MachineName, PortType, PortID, IP, Password, Com};
            var helper = new SqlHelper();
            return helper.ExecuteNonQuery(myTransaction, "DIC_MACHINE_Update", myParams, myValues);
        }
    }
}